package com.castbefore.mobile.android;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class TaskAdapter extends ArrayAdapter<com.google.api.services.tasks.model.Task> {
	private ArrayList<com.google.api.services.tasks.model.Task> tasks;
	private LayoutInflater inflater;
	
	public TaskAdapter(Context context, int resource,
			ArrayList<com.google.api.services.tasks.model.Task> objects,LayoutInflater inflater) {
		super(context, resource, objects);
		// TODO Auto-generated constructor stub
		this.tasks=objects;
		this.inflater=inflater;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v=convertView;
		if(v==null){
			v=inflater.inflate(R.layout.list_item, null);
		}
		com.google.api.services.tasks.model.Task task=tasks.get(position);
	        Logger.getLogger(ChooseGoogleAccountActivity.class.getName()).log(Level.INFO, "inside taskadapter");
		if(task!=null){
			TextView title=(TextView)v.findViewById(R.id.title);
			TextView dueDate=(TextView)v.findViewById(R.id.dueDate);
			TextView note=(TextView)v.findViewById(R.id.note);
			if(title!=null){
				title.setText(task.getTitle());
			}
			if(dueDate!=null){
				if(task.getDue()!=null){
					Date due=new Date(task.getDue().getValue());
					dueDate.setText(new SimpleDateFormat("MMM dd, yyyy").format(due));
				}else{
					dueDate.setText("");
				}
			}
			if(note!=null){
				String text=task.getNotes();
				note.setText((text.length()>=30?text.substring(0, 30):text));
			}
		}
		return v;
	}
	
}
